<?php
declare (strict_types=1);

namespace app\command;


use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;
use think\facade\Db;

class MaterialSync extends Command
{

    protected function configure()
    {
        // 指令配置
        $this->setName('material-sync')
            ->setDescription('素材同步');
    }


    /**
     * 命令行的执行。
     * @param Input $input
     * @param Output $output
     */
    protected function execute(Input $input, Output $output)
    {
        $AppKey =config('hugeamount.apiKey');
        if(empty($AppKey)) {
            return [];
        }
        $redis = Cache::store('redis')->handler();
        $redis->select(5);
        //获取数据
        $redisYesterdayData =$data=  [];
        for($i = 0;$i < count($AppKey);$i++) {
            $data_yesterday=$redis->get('oceanengine_business_material_analysis_data_yesterday:'.$AppKey[$i]);
            if($data_yesterday){
                $data_yesterday = json_decode($data_yesterday, true);
            }
            if(!empty($data_yesterday)) {
                $redisYesterdayData =  $data_yesterday['data'];
                foreach($redisYesterdayData as $k => $val) {
                    $key=$AppKey[$i].'_'.$k;
                    $data[$key]['company_key'] = (string)$AppKey[$i];
                    $data[$key]['company_name'] = $data_yesterday['account_name'];
                    $data[$key]['material_id'] = (string)$k;
                    $data[$key]['created_at'] = $val['created_at'];//创建时间
                    $data[$key]['title'] = $val['title'];//素材名称
                    $data[$key]['show_cnt'] = $val['show_cnt'];//展示数
                    $data[$key]['click_cnt'] = $val['click_cnt'];//点击数
                    $data[$key]['ctr'] = $val['ctr'];//点击率
                    $data[$key]['active_cost'] = $val['active_cost'];//激活成本
                    $data[$key]['active'] = $val['active'];//激活数
                    $data[$key]['cpc_platform'] = $val['cpc_platform'];//平均客单价
                    $data[$key]['conversion_cost'] = $val['conversion_cost'];//平均转化成本
                    $data[$key]['convert_cnt'] = $val['convert_cnt'];//转化数
                    $data[$key]['conversion_rate'] = $val['conversion_rate'];//转化率
                    $data[$key]['related_ad_cnt'] = $val['related_ad_cnt'];//关联计划数
                    $data[$key]['related_creative_cnt'] = $val['related_creative_cnt'];//关联创意数
                    $data[$key]['related_promotion_cnt'] = $val['related_promotion_cnt'];//关联广告数
                    $data[$key]['stat_cost'] = $val['stat_cost'];//消耗
                    $data[$key]['create_time']=time();
                    $data[$key]['platform'] = 1;
                    $data[$key]['date'] = date('Ymd',strtotime(date('Y-m-d H:i:s') . " -1 day"));
                    $data[$key]['month'] = date('m',strtotime(date('Y-m-d H:i:s') . " -1 day"));
                }
            }
        }
    
        $data = array_values($data);
        Db::name('data_material')->insertAll($data);
        return true;
    }


}
